#{extends 'main.html'/}
<script type="text/javascript">
   //user,dept初始值,放在最外面，让所有方法都能读取
    var groupicon = "public/ligerUI/skins/icons/communication.gif";
    $(function () {
        //var tag=${tag};

        var grid ,c_grid;
        var detailWin = null;
        var curentData = null;
        var currentIsAddNew =  null;
	    var mainform = $("#mainform");
	    
	    var c_detailWin = null,projectRow=null;
	    
	    var c_detailForm = $("#contractForm");
        var queryform = $("#queryform");
        var qf;
        queryform.ligerForm({
            inputWidth: 180, labelWidth: 70, space: 10,
            fields: [
            { name: "ProductID", type: "hidden" },
            { display: "项目名称", name: "productName", newline: false, type: "text",height:30,
            	group: "工程信息查询", groupicon: groupicon}, 
            { display: "合同单位", name: "contractUnit", newline: false, height:30,
            		comboboxName: "CompanyName" }
            ]
        });
        
        var queryButton = null;
        queryButton = $("#queryButton").ligerButton(
            {
                click: function (){
                    $(queryform).submit();
                },
                width: 50, text: '查询'
            }
        );
        /**/
        $(queryform).bind("submit",function(){
        	var productName =$("#productName").val();
        	var contractUnit = $("#contractUnit").val();
        	var action = $(queryform).attr("action");
        	$.getJSON(action,{productName:productName,contractUnit:contractUnit},function(data){
        		grid.loadData(data);
        	});
        	return false;
        })

        
        
        function f_initGrid()
        { 
            grid = $("#maingrid").ligerGrid({
                columns: [
                    { display: 'id', name: 'id', width: 5,  hide: true},
                    { display: '项目名称', name: 'name', width: 200, type: 'string', align:'left'},
                    { display: '合同单位', name: 'contractUnit', width: 150, type: 'string', align:'left'},
                    { display: '合同总价', name: 'contractAmount', width: 100, type: 'string', align:'left'},
                    { display: '累计到位资金', name: 'paidIn', width: 100, type: 'string', align:'left'},
                    { display: '累计拨付资金', name: 'appropriate', width: 100, type: 'string', align:'left'},
                    { display: '到位资金差额', name: 'paidInDiff', width: 100, type: 'string', align:'left'},
                    { display: '拨付资金差额', name: 'appropriateDiff', width: 100, type: 'string', align:'left'},
                    { display: '备注', name: 'remark', width: 150, type: 'text', align:'left'}
                ],
                frozen:false,
                url: "/projects/json",
				method: "GET",                
                //detail: { onShowDetail: f_showNote,onExtend:f_showNote,height:'auto' },
                title:"输变电工程合同执行情况统计表",
                width: 1200, 
                pageSize: 30,
                height:400,
				rownumbers:false,
				checkbox: false,
				detail: {
                    onShowDetail: f_showContract,
                    onCollapse:f_hiddenContract,
                    onExtend:f_contractUnfold,
                    height:310,width:"100%"},
                toolbar: { items: [
                        { text: '添加', click: addRow, icon: 'add' },
                        { line: true },
                        { text: '修改', click: modifyRow, icon: 'modify' },
                        { line: true },
                        { text: '删除', click: deleteRow, icon: 'delete' }
                    ] }
            });
                 
        }
        
        //初始化工程表格
        f_initGrid();
        

      
        //添加工程
        function addRow( )
        {  
			showDetail({},false);
        }
        //添加工程初始化 表单
        function showDetail(data,isAddNew){

            currentData = data;
            currentIsAddNew = isAddNew;
            if (detailWin)
            {
                detailWin.show(); 
            }
            else
            {
                //创建表单结构
	            mainform.ligerForm({
	                
	                fields: [
	           			{ name: "project.id", type: "hidden" },
	           			{ display: "项目名称", name: "project.name", newline: true, 
	           			  labelWidth: 100, width: 220, space: 30, type: "text", 
	           			  validate: { required: true, maxlength: 50} },
	           			{ display: "合同单位", name: "project.contractUnit", newline: true, 
	                  		labelWidth: 100, width: 220, space: 30, type: "text",
	                  		validate: { required: true, maxlength: 50} },
	           			{ display: "合同总价", name: "project.contractAmount", newline: true, 
	                        labelWidth: 100, width: 220, space: 30, type: "text",
	                        validate:{required:true,maxlength:20}},
	           			{ display: "累计到位资金", name: "project.paidIn", newline: true, 
	                        labelWidth: 100, width: 220, space: 30, type: "text",
	                        validate:{required:true,maxlength:20,digits:true}},
	           			{ display: "累计拨付资金", name: "project.appropriate", newline: true, 
	                        labelWidth: 100, width: 220, space: 30, type: "text",
	                        validate:{required:true,maxlength:20}},
	           			{ display: "到位资金差额", name: "project.paidInDiff", newline: true, 
	                        labelWidth: 100, width: 220, space: 30, type: "text",
	                        validate:{required:true,maxlength:20}},
	           			{ display: "拨付资金差额", name: "project.appropriateDiff", newline: true, 
	                        labelWidth: 100, width: 220, space: 30, type: "text",
	                        validate:{required:true,maxlength:20}},
	           			{ display: "备注", name: "project.remark", newline: true, 
	                        labelWidth: 100, width: 220, space: 30, type: 'text', type: 'textarea',
	                        validate:{required:false,maxlength:200}}
	                                                       
	                ],       			
	                    toJSON: JSON2.stringify
	                });            

                detailWin = $.ligerDialog.open({
                    target: $("#detail"),
                    width: 450, height:350, top:80,
                    buttons: [
                    { text: '确定', onclick: function () { $("#mainform").submit(); } },
                    { text: '取消', onclick: function () { detailWin.hide(); } }
                    ]
                });
            }
            
            if (currentData)
            {   
            	$("[name$=id]").val(currentData.id);
            	$("[name$=name]").val(currentData.name);
                $("[name$=contractUnit]").val(currentData.contractUnit);
                $("[name$=contractAmount]").val(currentData.contractAmount);
				$("[name$=paidIn]").val(currentData.paidIn);
                $("[name$=appropriate]").val(currentData.appropriate);
                $("[name$=paidInDiff]").val(currentData.paidInDiff);
                $("[name$=appropriateDiff]").val(currentData.appropriateDiff);
                $("[name$=remark]").val(currentData.remark);           
                $("[name$=name]").focus();
            }
            
            formSumit({
        		  formId : "mainform",
        		  url :'/projects/add',
        		  grid:grid,
        		  dialog :detailWin,
        		  sumitSuccess :null,
        		  beforeSubmit : null
        	  });

        }
        
        
        //修改工程
        function modifyRow()
        {         
            var selected =   grid.getSelected();
            if (!selected) { LG.tip('请选择行!'); return }
            
            showDetail({
                id: selected.id,
                name:selected.name,
                contractUnit:selected.contractUnit,
                contractAmount:selected.contractAmount,
                paidIn:selected.paidIn,
                appropriate:selected.appropriate,
                paidInDiff:selected.paidInDiff,
                appropriateDiff:selected.appropriateDiff,
                remark:selected.remark
            }, false);         
        }
        
     	//删除工程
        function deleteRow()
        {
        	f_delete({
        		selected:grid.getSelected(),
        		grid:grid,
        		url:"/projects/delete?id="
        	});
        }
        
//合同
        function f_showContract(row, detailPanel){
            $(detailPanel).empty();
            $(detailPanel).children("div").remove("div");
        	projectRow = row;
        	var c_detial = document.createElement('div'); 
        	$(detailPanel).append(c_detial);
        	createContractGrig(c_detial,projectRow);
        }
        //关闭明细
        function f_hiddenContract(row ,detailPanel){
            projectRow=null;
            c_grid=null;
        }
        //展开明细
        function f_contractUnfold(row ,detailPanel){
            f_showContract(row,detailPanel);
        }
        //创建contract表格
        function createContractGrig(c_detial,row){
            c_grid = $(c_detial).css('margin',1).ligerGrid({
                columns:
                   [
                        { display: 'id', name: 'id',width:5, hide: true},
                        { display: 'projectId', name: 'projectId',width:5, hide: true},
                        { display: '合同编号', name: 'contractNum',width: 120,type:'string' },
                        { display: '乙方单位名称', name: 'partB', width: 190,type:'string' },
                        { display: '合同标的', name: 'subject',width: 100,type:'string'},
                        { display: '合同数量', name: 'count',width: 50,type:'string'},
                        { display: '合同价', name: 'price',width:50,type:'string'},
                        { display: '确定方式', name: 'selectedMode',width:100,type:"string" },
                        { display: '签订日期', name: 'SignedTime',width:80,type:"string"},
                        { display: '合同变更解除情况', name: 'changeInf',width:200,type:"string"},
                        { display: '备注', name: 'remark',width:150,type:'string'}
                   ], 
                frozen:false,
                width:"100%",
                height:200,
                url:"/projects/getContract?projectId="+row.id,
                //data:getContract(row.id),
                usePager:false,
                //pageSize:5,
                //onAfterShowData: callback,
                toolbar: { items: [
                             { text: '添加', click: addRow_c, icon: 'add' },
                             { line: true },
                             { text: '修改', click: modifyRow_c, icon: 'modify' },
                             { line: true },
                             { text: '删除', click: deleteRow_c, icon: 'delete' }
                     ] }

            });  
        }
        
        function addRow_c(){
        	show_c_detail({projectId:projectRow.id},false);
        }

        function modifyRow_c(){
        	 var selected = c_grid.getSelected();
        	 if(selected){
        		 show_c_detail(
   				 {
   					 id:selected.id,
   					 projectId:selected.projectId,
   					 contractNum:selected.contractNum,
   					 partB:selected.partB,
   					 subject:selected.subject,
   					 count:selected.count,
   					 price:selected.price,
   					 selectedMode:selected.selectedMode,
   					 SignedTime:selected.SignedTime,
   					 changeInf:selected.changeInf,
   					 remark:selected.remark
   				 },
   				 false);
        	 }else{
        		 LG.tip('请选择行');
        	 }
        }
        
        

        function deleteRow_c(){
        	
        	f_delete({
        		selected:c_grid.getSelected(),
        		grid:c_grid,
        		url:"/contract/delete?id="
        	});
        }
        
	   //弹出合同表单（添加/修改）
       function show_c_detail(contractData,isAddNew){

           if(c_detailWin){
               c_detailWin.show(); 
           }else{
	           //创建表单结构
	           c_detailForm.ligerForm({
                
                fields: [
					{ display:"id",name: "contract.id",id:"contractId", type: "hidden" },
           			{ display:"projectId",name: "contract.project.id",id:"projectId", type: "hidden" },
           			{ display: "合同编号", name: "contract.contractNum", newline: true, 
           			  labelWidth: 120, width: 220, space: 30, type: "text", 
           			  validate: { required: true, maxlength: 50} },
           			{ display: "乙方单位名称", name: "contract.partB", newline: true, 
                  		labelWidth: 120, width: 220, space: 30, type: "text",
                  		validate: { required: true, maxlength: 50} },
           			{ display: "签订日期", name: "contract.SignedTime", newline: true, 
                        labelWidth: 120, width: 220, space: 30, type: "date",
                        validate:{required:true,maxlength:20}},
           			{ display: "合同标的", name: "contract.subject", newline: true, 
                        labelWidth: 120, width: 220, space: 30, type: "text",
                        validate:{required:true,min:1,maxlength:20,digits:true}},
           			{ display: "合同数量", name: "contract.count", newline: true, 
                        labelWidth: 120, width: 220, space: 30, type: "digits",
                        validate:{required:true,maxlength:20}},
           			{ display: "合同价", name: "contract.price", newline: true, 
                        labelWidth: 120, width: 220, space: 30, type: "number",
                        validate:{required:true,maxlength:20}},
           			{ display: "确定方式", name: "contract.selectedMode", newline: true, 
                        labelWidth: 120, width: 220, space: 30, type: "text",
                        validate:{required:true,maxlength:20}},
               		{ display: "合同变更解除情况", name: "contract.changeInf", newline: true, 
                            labelWidth: 120, width: 220, space: 30, type: "text",
                            validate:{required:false,maxlength:100}},
           			{ display: "备注", name: "contract.remark", newline: true, 
                        labelWidth: 120, width: 220, space: 30, type: 'textarea',
                        validate:{required:false,maxlength:100}}
                                                       
                ],       			
                    toJSON: JSON2.stringify
                });     

               c_detailWin = $.ligerDialog.open({
                   target: $("#contractDetail"),
                   width: 480, height:400, top:80,
                   buttons: [
	                   { text: '确定', onclick: function () { save(); } },
	                   { text: '取消', onclick: function () { c_detailWin.hide(); } }
                   ]
               });
           }
           
           if(contractData){
        	  	$("[id='contractId']").val(contractData.id);
				$("[id='projectId']").val(contractData.projectId);
				$("[name$=contractNum]").val(contractData.contractNum);
				$("[name$=partB]").val(contractData.partB);
				$("[name$=SignedTime]").val(contractData.SignedTime);
				$("[name$=subject]").val(contractData.subject);
				$("[name$=count]").val(contractData.count);
				$("[name$=price]").val(contractData.price);
				$("[name$=selectedMode]").val(contractData.selectedMode);
				$("[name$=changeInf]").val(contractData.changeInf);
				$("[name$=remark]").val(contractData.remark);          
				$("[name$=name]").focus();
           }
           /*
           formSumit({
     		  formId : "contractForm",
     		  url :'/contract/save',
     		  grid:c_grid,
     		  dialog :c_detailWin,
     		  sumitSuccess :null,
     		  beforeSubmit : null
     	  });
           */
           
           function save(){
        	   //$("#contractForm").validate();
               
               $.ajax({
                   loading: '正在保存数据中...',
                   type: 'POST',
                   url: '/contract/save',                  
                   data: $("#contractForm").serialize(),
                   success: function ()
                   {
                	   c_detailWin.hide();
                	   c_grid.loadData(true);
                       LG.tip('保存成功!');
                   },
                   error: function (message)
                   {
                       LG.tip(message);
                   }
               });
           }
       }
    
		
		
		
    })   
</script>

<div id="query">
    <form id = "queryform" method="post" action="/projects/serach"></form>
    <div id="queryButton"></div>
</div>
<div id="maingrid" style="margin-top:10px"></div> <br />
<div style="display:none;"></div>
<div id="detail"   style="display:none;">
    <form id="mainform" column="300" method="post"></form>
</div>

<div id="contractDetail" style="display:none">
	<form id="contractForm" column="300" method="post"></form>
</div>
